package com.yuhang.learnandroid.chaper05.quiz

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.yuhang.learnandroid.R
import com.yuhang.learnandroid.chaper05.quiz.Question
import kotlinx.android.synthetic.main.activity_quiz5.*

class Quiz5Activity : AppCompatActivity() {
    private val TAG = "Quiz2Activity"
    private val KEY_INDEX="index"
    private val EXTRA_ANSWER_IS_TRUE = "com.answer_is_true"
    private val EXTRA_ANSWER_SHOWN = "com.answer_shown"
    private val REQUEST_CODE_CHEAT = 0

    private val mQuestionBank: Array<Question> =
            arrayOf(
                    Question(R.string.q_oceans_text, true),
                    Question(R.string.q_mideast_text, false),
                    Question(R.string.q_africa_text, false),
                    Question(R.string.q_americas_text, true),
                    Question(R.string.q_asia_text,true))
    private var mCurrentIndex = 0
    private var isCheater = false

    private var isCheaterArr = arrayOf(false, false, false, false,false)



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_quiz5)

        if(savedInstanceState != null){
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0)
        }

        updateQuestion()

        previous_button.setOnClickListener{v ->
            mCurrentIndex = (mQuestionBank.size + mCurrentIndex - 1) % mQuestionBank.size
            updateQuestion()
        }
        next_button.setOnClickListener{v ->
            mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.size
            updateQuestion()
            ///isCheater = false
        }

        question_text_view.setOnClickListener{ v ->
            mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.size
            updateQuestion()
            ///isCheater = false
        }

        true_button.setOnClickListener { checkAnswer(mCurrentIndex,true)}
        false_button.setOnClickListener { checkAnswer(mCurrentIndex,false)}

        var self = this
        cheat_button.setOnClickListener {
            var i = newIntent(self,mCurrentIndex,  mQuestionBank[mCurrentIndex].answerTrue)
            startActivityForResult(i, REQUEST_CODE_CHEAT)
        }
    }

    override protected fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode != Activity.RESULT_OK) return

        if(requestCode == REQUEST_CODE_CHEAT){
            if(data == null){
                return
            }
            //isCheater = wasAnswerShown(data)
            var isCheat = data.getBooleanExtra(EXTRA_ANSWER_SHOWN, false)
            var idx = data.getIntExtra("dix", 0)
            isCheaterArr[idx] = isCheat
        }
    }

    private fun wasAnswerShown(result: Intent): Boolean {
        return result.getBooleanExtra(EXTRA_ANSWER_SHOWN, false)
    }

    private fun newIntent(ctx: Context,idx: Int , answerIsTrue: Boolean): Intent{
        var i = Intent(ctx, CheatActivity::class.java)
        i.putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue)
        i.putExtra("idx", idx)
        return i
    }

    private fun updateQuestion() {
        var q = mQuestionBank[mCurrentIndex].mTextResId
        question_text_view.setText(q)
    }

    private fun checkAnswer(idx:Int ,pressedTrue: Boolean){
        var answerIsTrue = mQuestionBank[mCurrentIndex].answerTrue
        var resId = if(isCheaterArr[idx]) R.string.judgment_toast else {if(pressedTrue == answerIsTrue) R.string.correct_toast else R.string.incorrect_toast}

        Toast.makeText(this,resId,Toast.LENGTH_SHORT).show()
    }
}
